package com.rs.service.impl;

import com.rs.dao.UserDao;
import com.rs.entity.Class;
import com.rs.entity.Student;
import com.rs.entity.UserVo;
import com.rs.entity.login.Menu;
import com.rs.entity.login.User;
import com.rs.entity.vo.AddUserVO;
import com.rs.service.UserService;
import com.rs.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author dehang
 * @date 2019/6/1
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(String username, String password){

        return userDao.login(username, Md5Utils.getMD5(password, "utf-8"));

    }

    @Override
    public String getMenuIds(int id) {
        return userDao.getMenuIds(id);
    }

   @Override
    public List<Menu> getTwoMenu(String ids){

        return userDao.getTwoMenu(ids);
    }

   @Override
    public List<Menu> getOneMenu(String ids){
        //查询一级菜单id
        String oneIds=userDao.getOneIds(ids);

        return userDao.getOneMenu(oneIds);
    }

    @Override
    public List<UserVo> getUserList(long page, long limit,String name){

        List<UserVo> vos=userDao.getUserList((page-1)*limit,limit,name);

        return vos;
    }

    @Override
    public Long getUserSum(String name){

        return userDao.getUserSum(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addUser(AddUserVO addUserVO){

        //判断用户名是否存在
         int num=userDao.getNamenum(addUserVO.getUserName(),0);
         if(num>0){
             return -1;
         }
        try {
            //添加用户
            User user=new User();
            user.setUsername(addUserVO.getUserName());
            user.setNickName(addUserVO.getNickName());
            user.setPassword(Md5Utils.getMD5(addUserVO.getTwopwd(), "utf-8"));
            user.setEmail(addUserVO.getEmail());
            user.setBirthday(addUserVO.getBirthday());
            user.setPhone(addUserVO.getPhone());
            user.setSex(addUserVO.getSex());
            userDao.addUser(user);
            //添加权限
            userDao.addRoleUser(addUserVO.getRoleId(), user.getId());
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }


    @Override
    public UserVo getUpdateUser(int id){
        return userDao.getUpdateUser(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public long updateUser(AddUserVO addUserVO){
        //判断用户名是否存在
        int num=userDao.getNamenum(addUserVO.getUserName(), addUserVO.getId());
        if(num>0){
            return -1;
        }
        User user=new User();
        user.setId(addUserVO.getId());
        user.setUsername(addUserVO.getUserName());
        user.setNickName(addUserVO.getNickName());
        user.setEmail(addUserVO.getEmail());
        user.setBirthday(addUserVO.getBirthday());
        user.setPhone(addUserVO.getPhone());
        user.setSex(addUserVO.getSex());
        userDao.updateByPrimaryKeySelective(user);
        //删除员工现有角色
        userDao.delRoleUser(addUserVO.getId());
        //给员工添加角色
        userDao.addRoleUser(addUserVO.getRoleId(), user.getId());
        return 0;
    }


    /**
     * 操作权限
     * @param operate
     * @param user
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public User setOperate(String operate,User user){
        if(!StringUtils.isEmpty(operate)){
            String[] ss=operate.split(",");
        }
        return user;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delUser(int id){
        //删除员工
        userDao.delUser(id);
        //删除权限
        userDao.delRoleUser(id);
    }


    @Override
    public void reset(int id,String pwd){
        userDao.reset(id, Md5Utils.getMD5(pwd, "utf-8"));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changepwd(int id,String pwd,String oldpwd){
        //查询旧密码
        int size=userDao.oldpwd(id,Md5Utils.getMD5(oldpwd, "utf-8"));
        if(size==0){
            return -1;
        }
        userDao.restpwd(id,Md5Utils.getMD5(pwd, "utf-8"));

        return 0;
    }



    @Override
    public List<Class> getHaveStudent(long id){

        return userDao.getHaveStudent(id);
    }

    @Override
    public List<Class> getStudent(){
        return userDao.getStudent();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void allot(String mid,int id){
        String[] ids=mid.split(",");
        //删除该角色已有权限
        userDao.deletePower(id);
        //批量插入勾选的权限
        userDao.addPower(ids,id);
    }

}
